package com.ys.web.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ys.extend.utils.LogUtil;
import com.ys.web.dao.CommonDao;
import com.ys.web.entity.User;
import com.ys.web.remotemodel.AdOfflineInfo;
import com.ys.web.remotemodel.ContractInfo;
import com.ys.web.service.ContractInfoService;
import com.ys.web.ws.service.ManageContractInfoService;

/**
 * 合同信息-服务
 * @author wdj
 *
 */
@Service
@Transactional
public class ContractInfoServiceImpl implements ContractInfoService {
	@Autowired
	private CommonDao commonDao;
	/**远程调用-管理KAD系统所需的合同信息*/
	@Resource
	private ManageContractInfoService manageContractInfoService;
	
	@Override
	public List<ContractInfo> getContractInfoBySemUserId(Integer userIdKAD) {
		ContractInfo contractInfo = new ContractInfo();
		if(userIdKAD!=null){
			contractInfo.setUserIdKAD(userIdKAD);
		}
		
		List<ContractInfo> list = manageContractInfoService.getContractInfoByCondition(contractInfo);
//		List<ContractInfo> contractInfolist = new ArrayList<ContractInfo>();
		boolean flag = false;
		if(list!=null&&!list.isEmpty()){
			for(ContractInfo contract:list){
				if(contract.getUserIdKAD()!=null&&contract.getEmployeeName()==null){
					User user = commonDao.getEntity(User.class, contract.getUserIdKAD());	
					if(user!=null){
						contract.setEmployeeName(user.getEmployeeName());
						contract.setEmployeeNum(user.getEmployeeNumber());
						
					}
					manageContractInfoService.updateContractInfo(contract);
					flag = true;
				}
				
			}
		}
		if(flag){
			list = manageContractInfoService.getContractInfoByCondition(contractInfo);
		}
		
		return list;
	}

	@Override
	public void updateContractInfo(ContractInfo contractInfo) {
		// TODO Auto-generated method stub
		
		manageContractInfoService.updateContractInfo(contractInfo);
	}

	@Override
	public List<AdOfflineInfo> getAdOfflineInfoBySemUserId(Integer userIdKAD) {
		List<AdOfflineInfo> list = new ArrayList<>();
		List<AdOfflineInfo> adlist = manageContractInfoService.getAdOfflineInfoBySemUserId(userIdKAD);
		if(adlist!=null && !adlist.isEmpty()) {
			for (AdOfflineInfo adOfflineInfo : adlist) {
				try {
					
					if(adOfflineInfo.getUserIdKAD()!=null&&adOfflineInfo.getEmployeeName()==null){
						User user = commonDao.getEntity(User.class, adOfflineInfo.getUserIdKAD());
						
						adOfflineInfo.setEmployeeName(user.getEmployeeName());
						adOfflineInfo.setEmployeeNum(user.getEmployeeNumber());
						
						manageContractInfoService.updateContractInfo(adOfflineInfo);
					}
//					if(adOfflineInfo.getUserIdKAD()!=null) {
//						User user = commonDao.getEntity(User.class, adOfflineInfo.getUserIdKAD());
//						if(user!=null) {
							//暂时显示管理职员的名字
//							adOfflineInfo.setSemName(user.getEmployeeName());								
//						}
						
//					}
					
					list.add(adOfflineInfo);					
					
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		
		return list;
	}

}
